package com.myBlog.config;

import com.alibaba.fastjson2.JSONObject;
import com.myBlog.intercaptor.LoginInterceptor;
import com.myBlog.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Priority;

@Slf4j
@RestControllerAdvice
public class ExceptionGlobalAdvice {

    @ExceptionHandler(LoginInterceptor.LoginException.class)
    public JSONObject handleLoginException(LoginInterceptor.LoginException e){
        log.error(e.getMessage(), e);
        JSONObject jsonObject = JsonUtil.error_info(e.getInfo());
        jsonObject.put("code", e.getStatus());
        return jsonObject;
    }

    @ExceptionHandler(Exception.class)
    public JSONObject handleException(Exception e){
        log.error(e.getMessage(), e);
        return JsonUtil.error_info(e.getMessage());
    }
}
